/**
 * @description 地址
 * @author 赵子靖
 * @since 2017-12-17
 */
var Address = function() {
    var t = {
        path :'',
        init : function() {
            t.initView();
            t.initEvent();
        },
        initView : function() {
            
        },
        initEvent : function() {
            
        },
        //获取页面上移的距离  
        getScrollTop:function(){  
             var scrollTop=0;  
             if(document.documentElement&&document.documentElement.scrollTop){  
               scrollTop=document.documentElement.scrollTop;  
             }else if(document.body){  
               scrollTop=document.body.scrollTop;  
             }  
             return scrollTop;  
        },
        //地址列表遮罩
        addressListPop:function(){
            $("#address-list").load(t.path+"/mobile/memberAddress/forwardMemberListPop.do",function(responseTxt,statusTxt,xhr){
                if(statusTxt=="success"){
                    $("#page-shade").css("top",t.getScrollTop()+'px');  
                    $("#page-shade").show();  
                    $("#address-list-pop").css("bottom",-t.getScrollTop()+'px');  
                    $("#address-list-pop").show();  
                    $("body").css("overflow","hidden"); 
                    $(document).on('touchmove',function (e){
                        e.preventDefault();
                    });
                }else if(statusTxt=="error"){
                    layer.open({
                        title: [
                          '出错啦',
                          'background-color: #FF4351; color:#fff;'
                        ]
                        ,content: '出错啦。'+xhr.statusText
                    });
                }
            }); 
        },
        //关闭地址列表弹窗
        closeAddressListPop:function(){
            $("#page-shade").hide();  
            $("#address-list-pop").hide();  
            $("#address-list-pop").css("overflow","auto"); 
            $(document).unbind("touchmove");
        },
        //新增收货地址
        addAddressPop:function(){
            $("#address-add").load(t.path+"/mobile/memberAddress/forwardMemberAddPop.do",function(responseTxt,statusTxt,xhr){
                if(statusTxt=="success"){
                    t.closeAddressListPop();
                    $("#cityDiv").show();
                    $("#cityDivLine").show();
                    $("#page-shade").css("top",t.getScrollTop()+'px');  
                    $("#page-shade").show();  
                    $("#add-address-pop").css("bottom",-t.getScrollTop()+'px');  
                    $("#add-address-pop").show();  
                    $("body").css("overflow","hidden"); 
                    $(document).on('touchmove',function (e){
                        e.preventDefault();
                    });
                }else if(statusTxt=="error"){
                    layer.open({
                        title: [
                          '出错啦',
                          'background-color: #FF4351; color:#fff;'
                        ]
                        ,content: '出错啦。'+xhr.statusText
                    });
                }
            }); 
        },
        showAddressPop:function(){
            t.closeAddressListPop();
            $("#cityDiv").show();
            $("#cityDivLine").show();
            $("#page-shade").css("top",t.getScrollTop()+'px');  
            $("#page-shade").show();  
            $("#add-address-pop").css("bottom",-t.getScrollTop()+'px');  
            $("#add-address-pop").show();  
            $("body").css("overflow","hidden"); 
            $(document).on('touchmove',function (e){
                e.preventDefault();
            });
        },
        closeAddAddressPopOpenListPop:function(){
            $("#page-shade").hide();  
            $("#add-address-pop").hide();  
            $("#add-address-pop").css("overflow","auto");  
            $(document).unbind("touchmove");
            t.addressListPop();
        },
        closeAddAddressPop:function(){
            $("#page-shade").hide();  
            $("#add-address-pop").hide();  
            $("#add-address-pop").css("overflow","auto");  
            $(document).unbind("touchmove");
        },
        //新增地址
        saveAddress:function(){
            var name = $("#name").val().replace(/[ ]/g,"");
            var mobile=$("#mobile").val().replace(/[ ]/g,"");
            var sel_city = $("#sel_city").val();
            var address=$("#address").val().replace(/[ ]/g,"");
            var isMobile=/^(0?1[3578]\d{9})$|^((0(10|2[1-3]|[3-9]\d{2}))?[1-9]\d{6,7})$/;//联系方式验证
            var postCode=$("#postCode").val();
            if(Util.isEmpty(name)){
                layer.open({
                    content: '请输入收货人姓名'
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
                $("#name").focus();
                $("#nameSpan").html("请输入收货人姓名");
                return;
            }
            $("#nameSpan").html("");
            if(Util.isEmpty(mobile)){
                layer.open({
                    content: '请输入收货人手机'
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
                $("#mobile").focus();
                $("#mobileSpan").html("请输入收货人手机");
                return;
            }
            $("#mobileSpan").html("");
            if(!Util.isMobile(mobile)){
                layer.open({
                    content: '手机格式错误'
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
                $("#mobile").focus();
                $("#mobileSpan").html("手机格式错误");
                return;
            }
            $("#mobileSpan").html("");
            if(Util.isEmpty(sel_city)){
                layer.open({
                    content: '请选择收货地区'
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
                $("#sel_city").focus();
                $("#citySpan").html("请选择收货地区");
                return;
            }
            $("#citySpan").html("");
            if(Util.isEmpty(address)){
                layer.open({
                    content: '请输入详细地址'
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
                $("#address").focus();
                $("#addressSpan").html("请输入详细地址");
                return;
            }
            $("#addressSpan").html("");
            
            layer.open({
                type: 2
                ,content: '数据处理中'
            });
            $.ajax({
                type:"POST",
                url:t.path+"/mobile/memberAddress/saveMemberAddress.do",
                dataType:"json",
                data:{
                    name:name,
                    mobile:mobile,
                    city:sel_city,
                    address:address,
                    postCode:postCode
                },
                async: false,
                cache:false,
                success:function(data){
                    layer.closeAll();
                    if(data.success){
                        //添加成功
                        layer.open({
                            content: data.msg
                            ,skin: 'msg'
                            ,time: 2 //2秒后自动关闭
                        });
                        //刷新购物车数量
                        t.closeAddAddressPopOpenListPop();
                    }else{
                        //添加失败
                        layer.open({
                            title: [
                              '操作失败',
                              'background-color: #FF4351; color:#fff;'
                            ]
                            ,content: data.msg
                        });
                    }
                }
            });
        },
        //修改
        updateAddressPop:function(id){
            $("#address-add").load(t.path+"/mobile/memberAddress/forwardMemberUpdatePop.do?id="+id,function(responseTxt,statusTxt,xhr){
                if(statusTxt=="success"){
                    t.closeAddressListPop();
                    $("#cityDiv").show();
                    $("#cityDivLine").show();
                    $("#page-shade").css("top",t.getScrollTop()+'px');  
                    $("#page-shade").show();  
                    $("#add-address-pop").css("bottom",-t.getScrollTop()+'px');  
                    $("#add-address-pop").show();  
                    $("body").css("overflow","hidden"); 
                    $(document).on('touchmove',function (e){
                        e.preventDefault();
                    });
                }else if(statusTxt=="error"){
                    layer.open({
                        title: [
                          '出错啦',
                          'background-color: #FF4351; color:#fff;'
                        ]
                        ,content: '出错啦。'+xhr.statusText
                    });
                }
            }); 
        },
        //修改
        updateAddress:function(){
            var id = $("#id").val();
            var name = $("#name").val().replace(/[ ]/g,"");
            var mobile=$("#mobile").val().replace(/[ ]/g,"");
            var sel_city = $("#sel_city").val();
            var address=$("#address").val().replace(/[ ]/g,"");
            var isMobile=/^(0?1[3578]\d{9})$|^((0(10|2[1-3]|[3-9]\d{2}))?[1-9]\d{6,7})$/;//联系方式验证
            var postCode=$("#postCode").val();
            if(Util.isEmpty(name)){
                layer.open({
                    content: '请输入收货人姓名'
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
                $("#name").focus();
                $("#nameSpan").html("请输入收货人姓名");
                return;
            }
            $("#nameSpan").html("");
            if(Util.isEmpty(mobile)){
                layer.open({
                    content: '请输入收货人手机'
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
                $("#mobile").focus();
                $("#mobileSpan").html("请输入收货人手机");
                return;
            }
            $("#mobileSpan").html("");
            if(!Util.isMobile(mobile)){
                layer.open({
                    content: '手机格式错误'
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
                $("#mobile").focus();
                $("#mobileSpan").html("手机格式错误");
                return;
            }
            $("#mobileSpan").html("");
            if(Util.isEmpty(sel_city)){
                layer.open({
                    content: '请选择收货地区'
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
                $("#sel_city").focus();
                $("#citySpan").html("请选择收货地区");
                return;
            }
            $("#citySpan").html("");
            if(Util.isEmpty(address)){
                layer.open({
                    content: '请输入详细地址'
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
                $("#address").focus();
                $("#addressSpan").html("请输入详细地址");
                return;
            }
            $("#addressSpan").html("");
            
            layer.open({
                type: 2
                ,content: '数据处理中'
            });
            $.ajax({
                type:"POST",
                url:t.path+"/mobile/memberAddress/updateMemberAddress.do",
                dataType:"json",
                data:{
                    id:id,
                    name:name,
                    mobile:mobile,
                    city:sel_city,
                    address:address,
                    postCode:postCode
                },
                async: false,
                cache:false,
                success:function(data){
                    layer.closeAll();
                    if(data.success){
                        //添加成功
                        layer.open({
                            content: data.msg
                            ,skin: 'msg'
                            ,time: 2 //2秒后自动关闭
                        });
                        //刷新购物车数量
                        t.closeAddAddressPopOpenListPop();
                    }else{
                        //添加失败
                        layer.open({
                            title: [
                              '操作失败',
                              'background-color: #FF4351; color:#fff;'
                            ]
                            ,content: data.msg
                        });
                    }
                }
            });
        },
        locationAddress:function(maId,maName,maMobile,maCity,maAddress){
            $("#orderAddressId").val(maId);
            $("#orderReceivename").html(maName);
            $("#orderPhone").html(maMobile);
            $("#orderAddress").html(maCity+maAddress);
            t.closeAddressListPop();
        }
    }
    return t;
}();